﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MaterialDemo.IService.IPermission;
using MaterialDemo.Model.DepartmentDTO;
using MaterialDemo.Model.UserDTO;
using MaterialDemo.MVC.Filters;
using MaterialDemo.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace MaterialDemo.MVC.Controllers
{
    [MySession]
    [TypeFilter(typeof(MySqlPowerFilterAttribute))]
    //[TypeFilter(typeof(MyPowerFilterAttribute), Arguments = new object[] { "99" })]//{方式1：方法前使用} 如果在控制器上使用相同过滤器，此方式会失效（注意：如果需要在过滤器中注入参数，可以用TypeFilter）
    public class UserController : Controller
    {
        #region  构造函数注入
        private readonly ILogger<UserController> _logger;
        private readonly IUserService _userService;
        private readonly IDepartmentService _departmentService;

        public UserController(ILogger<UserController> logger, IUserService  userService, IDepartmentService departmentService)
        {
            _logger = logger;
            _userService = userService;
            _departmentService = departmentService;
        }
        #endregion

        #region 页面
        /// <summary>
        /// 员工信息主页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {

            return View();
        }
        /// <summary>
        /// 添加员工信息页面
        /// </summary>
        /// <returns></returns>
        public IActionResult AddIndex()
        {
            //【1】获取所有部门信息
            List<DepartmentOutput> list = _departmentService.GetList();
            ViewBag.departmentList = list;
            return View();
        }
        /// <summary>
        /// 修改员工信息页面
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IActionResult UpdateIndex(int id)
        {
            //【1】根据id获取用户信息
            UserOutput userEntity = _userService.LoadEntities(id).FirstOrDefault();
            if (userEntity != null)
            {
                ViewBag.model = userEntity;
            }
            //【2】获取部门信息
            List<DepartmentOutput> list = _departmentService.GetList();
            ViewBag.departmentList = list;
            return View();
        }
        #endregion




        #region 非页面
        /// <summary>
        /// 分页获取员工信息
        /// </summary>
        /// <returns></returns>
        public string GetJsonList(int page, int limit, string selectInfo)
        {
            int pageIndex = page;
            int pageSize = limit;
            int totalCount = 0;
            string mySelectInfo = selectInfo;

            List<UserOutput> outlist = _userService.LoadPagesEntities(pageIndex, pageSize, out totalCount, mySelectInfo);
            //序列化
            DataResult<List<UserOutput>> dataResult = new DataResult<List<UserOutput>>
            {
                code = 0,
                msg = "ok",
                count = totalCount,
                data = outlist,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }
        /// <summary>
        /// 查询用户是否存在方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string CheckRepeat(string value)
        {

            if (_userService.CheckRepeat(value))
            {
                //如果有记录，说明用户id重复，不可以
                return "no";
            }
            else
            {
                return "ok";
            }

        }
        /// <summary>
        /// 禁用和开启用户的方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string ForbidUser(int id, string flag)
        {

            if (_userService.ForbidUser(id, flag))
            {
                if (flag == "true")
                {
                    return "用户已开启";
                }
                else
                {
                    return "用户已禁用";
                }

            }
            else
            {
                return "操作失败";
            }

        }
        /// <summary>
        /// 密码重置
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string RePwd(int id)
        {

            if (_userService.RePwd(id))
            {
                return "密码重置成功，重置密码为123";
            }
            else
            {
                return "密码重置失败！";
            }
        }



        /// <summary>
        /// 信息添加方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public string Add(UserInput inputEntity)
        {
            if (_userService.Add(inputEntity) > 0)
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }
        /// <summary>
        /// 删除员工信息方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public string Delete(int id)
        {
            var ss = _userService.Delete(id);
            if (ss == true)
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 信息更新页面
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string Update(UserInput inputEntity)
        {

            if (_userService.Update(inputEntity) > 0)
            {
                return "ok";
            }
            else
            {
                return "no";
            }

        }
       
        #endregion

    }
}